using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;
using RecycleTray.Properties;

namespace RecycleTray
{
    public partial class Controller
    {
        #region Constants
        private const int NO_ITEMS = 0;
        private const int ONE_ITEM = 1;
        private const int TEN_ITEMS = 10;
        private const int ONE_SECOND = 1000;
        private const int TEN_SECONDS = 10000;
        #endregion

        #region Members
        private MainView _MainView;
        #endregion

        #region Methods
        public void MainView_Show()
        {
            this._MainView = new MainView();

            this._MainView.timerCheckBinStatus.Tick += 
                new EventHandler(MainView_timerCheckBinStatus_Tick);
            this._MainView.timerHideStatusWindow.Tick += 
                new EventHandler(MainView_timerHideStatusWindow_Tick);
            this._MainView.contextmenuMainView.Opening += 
                new CancelEventHandler(MainView_ContextMenu_Opening);
            this._MainView.menuitemAbout.Click += 
                new EventHandler(MainView_menuitemAbout_Click);
            this._MainView.menuitemEmpty.Click +=
                new EventHandler(MainView_menuitemEmpty_Click);
            this._MainView.menuitemExit.Click += 
                new EventHandler(MainView_menuitemExit_Click);
            this._MainView.NotifyIcon.DoubleClick += 
                new EventHandler(MainView_NotifyIcon_DoubleClick);

            this._AppContext.MainForm = this._MainView;

            this._MainView.timerCheckBinStatus.Interval = 
                ONE_SECOND;
            this._MainView.Left =
                Screen.PrimaryScreen.WorkingArea.Right - this._MainView.Width;
            this._MainView.Top =
                Screen.PrimaryScreen.Bounds.Bottom + this._MainView.Height;

            this._MainView.menuitemAbout.Text = String.Format(
                CultureInfo.CurrentCulture,
                "ne0ge0.com // {0}",
                DateTime.Now.Year);

            this._MainView.Show();
        }

        #region Event Handlers
        /// <summary>
        /// Timer-driven event which checks the contents of the Recycle Bin and changes the icon and action appropriately
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainView_timerCheckBinStatus_Tick(object sender, System.EventArgs e)
        {
            if (NativeMethods.RecycleBinCount.Equals(NO_ITEMS))
            {
                this._MainView.timerCheckBinStatus.Interval = ONE_SECOND;
            }
            else if (NativeMethods.RecycleBinCount > TEN_ITEMS)
            {
                this._MainView.timerCheckBinStatus.Interval = TEN_SECONDS;
            }
            else
            {
                this._MainView.timerCheckBinStatus.Interval =
                    NativeMethods.RecycleBinCount * ONE_SECOND;
            }

            this._MainView.NotifyIcon.Text = String.Format(
                CultureInfo.CurrentCulture,
                "RecycleTray - {0} item{1} in Recycle Bin",
                NativeMethods.RecycleBinCount,
                NativeMethods.RecycleBinCount.Equals(1) ? "" : "s");

            this._MainView.NotifyIcon.Icon = 
                (NativeMethods.RecycleBinCount > 0)
                    ? Resources.Application2
                    : Resources.Application;

            this._MainView.menuitemEmpty.Enabled = 
                (NativeMethods.RecycleBinCount > 0);
        }

        private void MainView_timerHideStatusWindow_Tick(object sender, EventArgs e)
        {
            this._MainView.Top = 
                Screen.PrimaryScreen.Bounds.Bottom + this._MainView.Height;

            Application.DoEvents();

            this._MainView.timerHideStatusWindow.Stop();
        }

        private void MainView_ContextMenu_Opening(object sender, CancelEventArgs e)
        {
            this._MainView.menuitemEmpty.Text = String.Format(
                CultureInfo.CurrentCulture,
                "&Empty {0} Item{1}",
                NativeMethods.RecycleBinCount,
                NativeMethods.RecycleBinCount.Equals(1) ? "" : "s");
        }

        private void MainView_menuitemAbout_Click(object sender, EventArgs e)
        {
            Process.Start("http://www.ne0ge0.com");
        }
        
        /// <summary>
        /// Empty Recycle Bin event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainView_menuitemEmpty_Click(object sender, EventArgs e)
        {
            Application.DoEvents();

            this._MainView.labelStatus.Text = String.Format(
                CultureInfo.CurrentCulture,
                "{0} item{1} deleted from Recycle Bin",
                NativeMethods.RecycleBinCount,
                NativeMethods.RecycleBinCount.Equals(ONE_ITEM) ? "" : "s");

            this._MainView.Top = 
                Screen.PrimaryScreen.WorkingArea.Bottom - this._MainView.Height;

            NativeMethods.EmptyRecycleBin();

            Application.DoEvents();

            this._MainView.timerHideStatusWindow.Start();
        }

		/// <summary>
		/// Exit event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void MainView_menuitemExit_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

        private void MainView_NotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            Process.Start(
                "explorer.exe",
                "::{645FF040-5081-101B-9F08-00AA002F954E}");
        }
        #endregion
        #endregion
    }
}
